package org.simple.framework.boot.controller;

import org.simple.framework.data.entity.Identifiable;
import org.simple.framework.web.result.base.SimpleResult;
import org.simple.framework.web.result.exception.web.ParameterException;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;

import jakarta.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：simple-framework
 * 类名称：ModelBatchDeleteController
 * 类描述：ModelBatchDeleteController
 * 创建时间：2018/9/7
 *
 * @author sisyphus   (E-mail:1620657419@qq.com)
 * @version v1.0
 */
public interface ModelBatchDeleteController<E extends Identifiable<ID>, ID extends Serializable> extends ModelController<E, ID> {

    void handleBatchDelete(List<E> entityList, Principal principal, HttpServletRequest request);

    @DeleteMapping("/batch")
    default SimpleResult batchDelete(@RequestBody List<ID> idList, Principal principal, HttpServletRequest request) {
        List<E> entityList = new ArrayList<>();
        idList.forEach(id -> entityList.add(getRepository().findById(id).orElseThrow(ParameterException::new)));
        handleBatchDelete(entityList, principal, request);
        getRepository().deleteAll(entityList);
        return SimpleResult.ok();
    }

}
